2020-2021 Sunseeker Telemetry and Lighting System
adc12_b_ex1_avccRef.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430FR59xx Demo - ADC12B, Sample A1, AVcc Ref, Set P1.0 if A1 > 0.5*AVcc
34 //
35 // Description: A single sample is made on A1 with reference to AVcc.
36 // Software sets ADC12BSC to start sample and conversion - ADC12BSC
37 // automatically cleared at EOC. ADC12B internal oscillator times sample (16x)
38 // and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC12B
39 // conversion complete, ADC12_B_ISR will force exit from LPM0 in Mainloop on
40 // reti. If A0 > 0.5*AVcc, P1.0 set, else reset. The full, correct handling of
41 // and ADC12B interrupt is shown as well.
42 //
43 //
44 // MSP430FR5969
45 // -----------------
46 // /|\| XIN|-
47 // | | |
48 // --|RST XOUT|-
49 // | |
50 // >---|P1.1/A1 P1.0|-->LED
51 //
52 //******************************************************************************
53 #include "driverlib.h"
54 
55 void main(void)
56 {
57  // Stop WDT
58  WDT_A_hold(WDT_A_BASE);
59 
60  //Set P1.0 as an output pin.
61  /*
62 
63  * Select Port 1
64  * Set Pin 0 as output
65  */
66  GPIO_setAsOutputPin(
67  GPIO_PORT_P1,
68  GPIO_PIN0
69  );
70 
71  //Set P1.0 as Output Low.
72  /*
73 
74  * Select Port 1
75  * Set Pin 0 to output Low.
76  */
78  GPIO_PORT_P1,
79  GPIO_PIN0
80  );
81  //Set P1.1 as Ternary Module Function Output.
82  /*
83 
84  * Select Port 1
85  * Set Pin 1 to output Ternary Module Function, (A1, C1, VREF+, VeREF+).
86  */
87  GPIO_setAsPeripheralModuleFunctionOutputPin(
88  GPIO_PORT_P1,
89  GPIO_PIN1,
90  GPIO_TERNARY_MODULE_FUNCTION
91  );
92 
93  /*
94  * Disable the GPIO power-on default high-impedance mode to activate
95  * previously configured port settings
96  */
97  PMM_unlockLPM5();
98 
99  //Initialize the ADC12B Module
100  /*
101  * Base address of ADC12B Module
102  * Use internal ADC12B bit as sample/hold signal to start conversion
103  * USE MODOSC 5MHZ Digital Oscillator as clock source
104  * Use default clock divider/pre-divider of 1
105  * Not use internal channel
106  */
107  ADC12_B_initParam initParam = {0};
108  initParam.sampleHoldSignalSourceSelect = ADC12_B_SAMPLEHOLDSOURCE_SC;
109  initParam.clockSourceSelect = ADC12_B_CLOCKSOURCE_ADC12OSC;
110  initParam.clockSourceDivider = ADC12_B_CLOCKDIVIDER_1;
111  initParam.clockSourcePredivider = ADC12_B_CLOCKPREDIVIDER__1;
112  initParam.internalChannelMap = ADC12_B_NOINTCH;
113  ADC12_B_init(ADC12_B_BASE, &initParam);
114 
115  //Enable the ADC12B module
116  ADC12_B_enable(ADC12_B_BASE);
117 
118  /*
119  * Base address of ADC12B Module
120  * For memory buffers 0-7 sample/hold for 64 clock cycles
121  * For memory buffers 8-15 sample/hold for 4 clock cycles (default)
122  * Disable Multiple Sampling
123  */
124  ADC12_B_setupSamplingTimer(ADC12_B_BASE,
125  ADC12_B_CYCLEHOLD_16_CYCLES,
126  ADC12_B_CYCLEHOLD_4_CYCLES,
127  ADC12_B_MULTIPLESAMPLESDISABLE);
128 
129  //Configure Memory Buffer
130  /*
131  * Base address of the ADC12B Module
132  * Configure memory buffer 0
133  * Map input A1 to memory buffer 0
134  * Vref+ = AVcc
135  * Vref- = AVss
136  * Memory buffer 0 is not the end of a sequence
137  */
138  ADC12_B_configureMemoryParam configureMemoryParam = {0};
139  configureMemoryParam.memoryBufferControlIndex = ADC12_B_MEMORY_0;
140  configureMemoryParam.inputSourceSelect = ADC12_B_INPUT_A1;
141  configureMemoryParam.refVoltageSourceSelect = ADC12_B_VREFPOS_AVCC_VREFNEG_VSS;
142  configureMemoryParam.endOfSequence = ADC12_B_NOTENDOFSEQUENCE;
143  configureMemoryParam.windowComparatorSelect = ADC12_B_WINDOW_COMPARATOR_DISABLE;
144  configureMemoryParam.differentialModeSelect = ADC12_B_DIFFERENTIAL_MODE_DISABLE;
145  ADC12_B_configureMemory(ADC12_B_BASE, &configureMemoryParam);
146 
147 
148  ADC12_B_clearInterrupt(ADC12_B_BASE,
149  0,
150  ADC12_B_IFG0
151  );
152 
153  //Enable memory buffer 0 interrupt
154  ADC12_B_enableInterrupt(ADC12_B_BASE,
155  ADC12_B_IE0,
156  0,
157  0);
158 
159 
160  while (1)
161  {
162  __delay_cycles(5000);
163 
164  //Enable/Start sampling and conversion
165  /*
166  * Base address of ADC12B Module
167  * Start the conversion into memory buffer 0
168  * Use the single-channel, single-conversion mode
169  */
170  ADC12_B_startConversion(ADC12_B_BASE,
171  ADC12_B_MEMORY_0,
172  ADC12_B_SINGLECHANNEL);
173 
174  __bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_B_ISR will force exit
175  __no_operation(); // For debugger
176  }
177 }
178 
179 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
180 #pragma vector=ADC12_VECTOR
181 __interrupt
182 #elif defined(__GNUC__)
183 __attribute__((interrupt(ADC12_VECTOR)))
184 #endif
185 void ADC12_ISR(void)
186 {
187  switch(__even_in_range(ADC12IV,12))
188  {
189  case 0: break; // Vector 0: No interrupt
190  case 2: break; // Vector 2: ADC12BMEMx Overflow
191  case 4: break; // Vector 4: Conversion time overflow
192  case 6: break; // Vector 6: ADC12BHI
193  case 8: break; // Vector 8: ADC12BLO
194  case 10: break; // Vector 10: ADC12BIN
195  case 12: // Vector 12: ADC12BMEM0 Interrupt
196  if (ADC12_B_getResults(ADC12_B_BASE, ADC12_B_MEMORY_0) >= 0x7ff)
197  {
198  //Set P1.0 LED on
199  /*
200 
201  * Select Port 1
202  * Set Pin 0 to output high.
203  */
205  GPIO_PORT_P1,
206  GPIO_PIN0
207  );
208  }
209  else
210  {
211  //Set P1.0 LED off
212  /*
213 
214  * Select Port 1
215  * Set Pin 0 to output high.
216  */
218  GPIO_PORT_P1,
219  GPIO_PIN0
220  );
221  }
222  __bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
223  break; // Clear CPUOFF bit from 0(SR)
224  case 14: break; // Vector 14: ADC12BMEM1
225  case 16: break; // Vector 16: ADC12BMEM2
226  case 18: break; // Vector 18: ADC12BMEM3
227  case 20: break; // Vector 20: ADC12BMEM4
228  case 22: break; // Vector 22: ADC12BMEM5
229  case 24: break; // Vector 24: ADC12BMEM6
230  case 26: break; // Vector 26: ADC12BMEM7
231  case 28: break; // Vector 28: ADC12BMEM8
232  case 30: break; // Vector 30: ADC12BMEM9
233  case 32: break; // Vector 32: ADC12BMEM10
234  case 34: break; // Vector 34: ADC12BMEM11
235  case 36: break; // Vector 36: ADC12BMEM12
236  case 38: break; // Vector 38: ADC12BMEM13
237  case 40: break; // Vector 40: ADC12BMEM14
238  case 42: break; // Vector 42: ADC12BMEM15
239  case 44: break; // Vector 44: ADC12BMEM16
240  case 46: break; // Vector 46: ADC12BMEM17
241  case 48: break; // Vector 48: ADC12BMEM18
242  case 50: break; // Vector 50: ADC12BMEM19
243  case 52: break; // Vector 52: ADC12BMEM20
244  case 54: break; // Vector 54: ADC12BMEM21
245  case 56: break; // Vector 56: ADC12BMEM22
246  case 58: break; // Vector 58: ADC12BMEM23
247  case 60: break; // Vector 60: ADC12BMEM24
248  case 62: break; // Vector 62: ADC12BMEM25
249  case 64: break; // Vector 64: ADC12BMEM26
250  case 66: break; // Vector 66: ADC12BMEM27
251  case 68: break; // Vector 68: ADC12BMEM28
252  case 70: break; // Vector 70: ADC12BMEM29
253  case 72: break; // Vector 72: ADC12BMEM30
254  case 74: break; // Vector 74: ADC12BMEM31
255  case 76: break; // Vector 76: ADC12BRDY
256  default: break;
257  }
258 }
void main(void)
void ADC12_ISR(void)
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)